package com.synjones.idcardusb_reader;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.widget.TextView;

import com.synjones.idcard.IDCard;

/**
 * Created by zhaodianbo on 2016-11-25.
 */
public class CardInfoView {
    private TextView textViewName;
    private TextView textViewSex;
    private TextView textViewNation;
    private TextView textViewBirthday;
    private TextView textViewAddress;
    private TextView textViewPIDNo;
    private TextView textViewGrantDept;
    private TextView textViewUserLife;
    private TextView tvFpAndAppendAdd;
    private ImageView imageViewPhoto ;
    Bitmap bmp;
    Activity mActivity;
    public CardInfoView(Activity activity){
        mActivity=activity;
        textViewName = (TextView) activity.findViewById(R.id.textViewName);
        textViewSex = (TextView) activity.findViewById(R.id.textViewSex);
        textViewNation = (TextView) activity.findViewById(R.id.textViewNation);
        textViewBirthday = (TextView) activity.findViewById(R.id.textViewBirthday);
        textViewAddress = (TextView) activity.findViewById(R.id.textViewAddress);
        textViewPIDNo = (TextView) activity.findViewById(R.id.textViewPIDNo);
        textViewGrantDept = (TextView) activity.findViewById(R.id.textViewGrantDept);
        textViewUserLife = (TextView) activity.findViewById(R.id.textViewUserLife);
        tvFpAndAppendAdd = (TextView) activity.findViewById(R.id.tvFpAndAppendAdd);
        imageViewPhoto = (ImageView) activity.findViewById(R.id.imageViewPhoto);
    }

    public void showCardView(IDCard idcard){
        if(idcard!=null){
            textViewName.setText(mActivity.getString(R.string.sdtname) + idcard.getName());
            textViewSex.setText(mActivity.getString(R.string.sdtsex) + idcard.getSex());
            textViewNation.setText(mActivity.getString(R.string.sdtnation) + idcard.getNation());
            textViewBirthday.setText(mActivity.getString(R.string.sdtbirthday)
                    + idcard.getYearOfBirth()	+ "年"
                    + idcard.getMonthOfBirth()	+ "月"
                    + idcard.getDayOfBirth()	+ "日");
            textViewAddress.setText(mActivity.getString(R.string.sdtaddress) + idcard.getAddress());
            textViewPIDNo.setText(mActivity.getString(R.string.sdtpidno) + idcard.getIDCardNo());
            textViewGrantDept.setText(mActivity.getString(R.string.sdtgrantdept) + idcard.getGrantDept());
            textViewUserLife.setText(mActivity.getString(R.string.sdtuserlife) + idcard.getUserLifBebinWithPoint() + "-" + idcard.getUserLifEndWithPoint());
            tvFpAndAppendAdd.setText(mActivity.getString(R.string.append_msg)+idcard.getFpDescription()+","+idcard.getAppendAddress());
            tvFpAndAppendAdd.append("\r\n读卡时间："+idcard.getReadTime());
            try {
                //int result = idcard.decodeResult;
                bmp=idcard.getPhoto();
                if (bmp==null) {
                    Resources res = mActivity.getResources();
                    bmp = BitmapFactory.decodeResource(res, R.drawable.photo);
                    //Log.e("状态：照片解码错");
                }
                imageViewPhoto.setImageBitmap(bmp);
                System.gc();

            } catch (Exception ioe) {
                ioe.printStackTrace();
            }

        }
        else{
            textViewName.setText(mActivity.getString(R.string.sdtname));
            textViewSex.setText(mActivity.getString(R.string.sdtsex));
            textViewNation.setText(mActivity.getString(R.string.sdtnation) );
            textViewBirthday.setText(mActivity.getString(R.string.sdtbirthday));
            textViewAddress.setText(mActivity.getString(R.string.sdtaddress));
            textViewPIDNo.setText(mActivity.getString(R.string.sdtpidno));
            textViewGrantDept.setText(mActivity.getString(R.string.sdtgrantdept) );
            textViewUserLife.setText(mActivity.getString(R.string.sdtuserlife) );
            tvFpAndAppendAdd.setText(mActivity.getString(R.string.append_msg));
            imageViewPhoto.setImageResource(R.drawable.photo);
        }
    }
}
